{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "8ceaadde",
   "metadata": {},
   "outputs": [],
   "source": [
    "import jieba\n",
    "import jieba.analyse"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6a461eb6",
   "metadata": {},
   "source": [
    "## 1. 分词模式,cut\n",
    "### 1.1 精确模式,cut_all"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "66c4a31b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "精确模式: 我/爱/自然语言/处理/技术\n"
     ]
    }
   ],
   "source": [
    "text = \"我爱自然语言处理技术\"\n",
    "seg_list = jieba.cut(text, cut_all=False)\n",
    "print(\"精确模式: \" + \"/\".join(seg_list))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3e3c3448",
   "metadata": {},
   "source": [
    "### 1.2 全模式"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "7d44b719",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "全模式: 我/爱/自然/自然语言/语言/处理/技术\n"
     ]
    }
   ],
   "source": [
    "seg_list = jieba.cut(text, cut_all=True)\n",
    "print(\"全模式: \" + \"/\".join(seg_list))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cf72aa34",
   "metadata": {},
   "source": [
    "### 1.3 搜索引擎模式\n",
    "先拆再挑有用的。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "9c2c3eb7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "搜索引擎模式: 我/爱/自然/语言/自然语言/处理/技术\n"
     ]
    }
   ],
   "source": [
    "seg_list = jieba.cut_for_search(text)\n",
    "print(\"搜索引擎模式: \" + \"/\".join(seg_list))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bb06c5dd",
   "metadata": {},
   "source": [
    "## 2. 操作"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c3fc79f6",
   "metadata": {},
   "source": [
    "### 2.4 lcut与cut"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d5a061bc",
   "metadata": {},
   "source": [
    "lcut返回列表  \n",
    "cut返回生成器，要join一下"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7a1102a4",
   "metadata": {},
   "source": [
    "### 2.5 自定义词典"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9328ba7b",
   "metadata": {},
   "source": [
    "## 3. 关键词提取,analyse"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "e30bab43",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Building prefix dict from the default dictionary ...\n",
      "Loading model from cache C:\\Users\\ZHENGY~1\\AppData\\Local\\Temp\\jieba.cache\n",
      "Loading model cost 0.464 seconds.\n",
      "Prefix dict has been built successfully.\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "关键词: ['学习', '机器', '人工智能', '分支', '深度']\n",
      "TextRank关键词: ['学习', '机器', '深度', '分支', '人工智能']\n"
     ]
    }
   ],
   "source": [
    "text2 = \"机器学习是人工智能的重要分支，深度学习是机器学习的一个子领域\"\n",
    "\n",
    "# 基于TF-IDF算法的关键词提取\n",
    "keywords = jieba.analyse.extract_tags(text2, topK=5)\n",
    "print(\"关键词:\", keywords)\n",
    "\n",
    "# 基于TextRank算法的关键词提取\n",
    "keywords_tr = jieba.analyse.textrank(text2, topK=5)\n",
    "print(\"TextRank关键词:\", keywords_tr)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9dde6def",
   "metadata": {},
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python (NLP)",
   "language": "python",
   "name": "inner_nlp"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
